(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.resourceBundle.viewController', viewController);
	viewController.$inject = [ '$scope', 'Constants', 'layoutConfig', 'validatorConfig', 'gridService', 'authService', 'resourceBundleService', '$filter' ];
	function viewController($scope, Constants, layoutConfig, validatorConfig, gridService, authService, resourceBundleService, $filter) {
		var ctrl = this, resourceBundleInfo = $scope.$stateParams.params;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;

		ctrl.init = function() {
			authService.initialOption().then(function(response) {
				ctrl.localOptions = response.data.localList;
				ctrl.refBundleOptions = response.data.bundleList;
			});
			resourceBundleService.getResourceBundleScopeOptions().then(function(response) {
				ctrl.scopeOptions = response;
			});
			ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, null, sortChanged);
			ctrl.gridOptions.enableRowHeaderSelection = false;
			ctrl.gridOptions.data = 'vm.searchCriteria.list';
			ctrl.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
			ctrl.searchCriteria.resBundleId = resourceBundleInfo.resBundleId;
			ctrl.searchCriteria.refResBundleId = resourceBundleInfo.refBundleId;
			ctrl.resourceBundle = resourceBundleInfo;
			ctrl.search();
		};
		
		ctrl.search = function() {
			ctrl.searchCriteria.list = [];
			if(ctrl.searchCriteria.resBundleId){
				ctrl.searchCriteria.resBundleId = $filter('encode')(ctrl.searchCriteria.resBundleId);
			}
			authService.search(ctrl.searchCriteria).then(
			function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
			},angular.noop);
		};

		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}
		
		function sortChanged(sortColumns) {
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			ctrl.search();
		}
	}
})(angular);